﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClassUI.entity;
using EasyClassUI.WebRequest;

namespace EasyClassUI.forms.group
{
    public partial class AddToGroup : Form
    {
        public static AddToGroup addToGroup;
        public AddToGroup()
        {
            InitializeComponent();
            errMsgLabel.Visible = false;
        }

        public void setErr(string errMsg) {
            errMsgLabel.Visible = true;
            errMsgLabel.Text = errMsg;
        }
        public void initGroupProfiles(List<StudyGroup> groups)
        {
            groupProfilesPanel.Controls.Clear();
            foreach(StudyGroup group in groups)
            {
                GroupProfile groupProfile = new GroupProfile(group.StudyGroupName, group.Description, group, true);
                groupProfilesPanel.Controls.Add(groupProfile);
            }
        }

        private void confirmGroup_Click(object sender, EventArgs e) {
            GroupService groupService = new GroupService();
            if (SearchText.Text == "") {
                setErr("Please enter your keyword");
                return;
            }
            QueryGroupStatus resultStatus = groupService.SearchGroupByName(SearchText.Text);
            if (resultStatus.Status == "200") {
                List<StudyGroup> result = resultStatus.Groups.Where(p => !StudyGroup.isInGroupIjoined(p.StudyGroupID) && p.StudyGroupName.ToLower().Contains(SearchText.Text.ToLower())).ToList();
                initGroupProfiles(result);
            }
            else {
                setErr("Something went wrong, try to search again or check your networks.");
            }
        }

        private void CloseButton_Click(object sender, EventArgs e) {
            groupUC.groupView.showAllMyAddedGs();
            this.Close();
        }
    }
}
